

di "************************************************************************************************"
di "*************************************** LOAD DATA ********** ***********************************"
di "************************************************************************************************"


u "$saveddata/RESTAT_replication.dta", clear


noi di "Define figure sample - based on those ever in regressions (keeping singletons)"
reghdfe ln_ees_ent  multiplant fgn_ent  ln_age_ent  if year <= 2015  & cloud!=. & ln_ees_ent!=.  & year>=2008 & ever_move==., absorb(ruref year ) cluster(ruref)		
gen sample =e(sample)
bysort ruref: egen ever_sample = max(sample)

label var sample					"Dummy = 1 for figure sample (keeping singletons)"
label var ever_sample				"Dummy = 1 if firm is ever in figure sample"

cd $output_location

di "************************************************************************************************"
di "***************************************Figures ************************************************"
di "************************************************************************************************"


noi di "Figure 3 - Firm crow-flies distance to the local telephone exchange"
sum exchange_distance_ent if year <= 2015 & year>=2008 &  maxsmpl_reg>=2 & smpl_reg==1  ,det
tab dist_Fig3 if year <= 2015 & year>=2008 &  maxsmpl_reg>=2 & smpl_reg==1 
tabstat exchange_distance_ent if year <= 2015 & year>=2008 &  maxsmpl_reg>=2 & smpl_reg==1 ,by(dist_Fig3) s(mean, count)
histogram exchange_dist if year <= 2015 & year>=2008 &  maxsmpl_reg>=2 & smpl_reg==1, fraction normal xlabel(#20)
graph export "Figure3.png", replace  as(png) 


noi di "Figure 4 - Employment pre-trends with timing of fiber enablement"
* Figure 4a
* young and incumbent together
xtset ruref year
csdid ln_ees_ent if ever_sample == 1  & young25_08==1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(ds1)
estimates store k1
csdid ln_ees_ent if ever_sample == 1 &   young25_08==0, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(ds2)
estimates store k2
event_plot ds1 ds2, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.15(0.05)0.15) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "Figure4a.png", replace  as(png) name("Graph") 

* Figure 4b
* near and far together
xtset ruref year
csdid ln_ees_ent if ever_sample == 1 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(cs1)
estimates store f1
csdid ln_ees_ent if ever_sample == 1 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(cs2)
estimates store f2
event_plot cs1 cs2, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.30(0.05)0.30) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "Figure4b.png", replace  as(png) name("Graph") 

*Figure 4c
* young near and young far together
xtset ruref year
csdid ln_ees_ent if ever_sample == 1 & young25_08==1 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(es1)
estimates store g1
csdid ln_ees_ent if ever_sample == 1 & young25_08==1 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(es2)
estimates store g2
event_plot es1 es2, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.30(0.05)0.30) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "Figure4c.png", replace  as(png) name("Graph") 

*Figure 4d
* incumbent near and incumbent far together
xtset ruref year
csdid ln_ees_ent if ever_sample == 1 & young25_08==0 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(fs1)
estimates store h1
csdid ln_ees_ent if ever_sample == 1 & young25_08==0 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(fs2)
estimates store h2
event_plot fs1 fs2, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.10(0.05)0.10) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "Figure4d.png", replace  as(png) name("Graph") 


noi di "Figure A1 - Sales pre-trends with timing of fiber enablement"
*Figure A1a
* young and incumbent together
csdid ln_sales_ent if ever_sample == 1 &   young25_08==1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(ds3)
estimates store k3
csdid ln_sales_ent if ever_sample == 1 &   young25_08==0, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(ds4)
estimates store k4
event_plot ds3 ds4, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.15(0.05)0.15) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA1a.png", replace  as(png) name("Graph") 
*Figure A1b
* near and far together
csdid ln_sales_ent if ever_sample == 1 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(cs3)
estimates store f3
csdid ln_sales_ent if ever_sample == 1 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(cs4)
estimates store f4
event_plot cs3 cs4, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.10(0.05)0.10) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA1b.png", replace  as(png) name("Graph") 
*Figure A1c
* young near and young far together
csdid ln_sales_ent if ever_sample == 1 & young25_08==1 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(es3)
estimates store g3
csdid ln_sales_ent if ever_sample == 1 & young25_08==1 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(es4)
estimates store g4
event_plot es3 es4, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.30(0.05)0.30) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA1c.png", replace  as(png) name("Graph") 
*Figure A1d
* incumbent near and incumbent far together
csdid ln_sales_ent if ever_sample == 1 & young25_08==0 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(fs3)
estimates store h3
csdid ln_sales_ent if ever_sample == 1 & young25_08==0 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(fs4)
estimates store h4
event_plot fs3 fs4, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.10(0.05)0.10) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA1d.png", replace  as(png) name("Graph") 



noi di "Figure A2 - Labor productivity pre-trends with timing of fiber enablement"
*Figure A2a
* young and incumbent together
csdid ln_sales_worker if ever_sample == 1 &   young25_08==1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(ds5)
estimates store k5
csdid ln_sales_worker if ever_sample == 1 &   young25_08==0, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(ds6)
estimates store k6
event_plot ds5 ds6, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.15(0.05)0.15) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA2a.png", replace  as(png) name("Graph") 
*Figure A2b
* near and far together
csdid ln_sales_worker if ever_sample == 1 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(cs5)
estimates store f5
csdid ln_sales_worker if ever_sample == 1 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(cs6)
estimates store f6
event_plot cs5 cs6, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.10(0.05)0.10) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA2b.png", replace  as(png) name("Graph") 
*Figure A2c
* young near and young far together
csdid ln_sales_worker if ever_sample == 1 & young25_08==1 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(es5)
estimates store g5
csdid ln_sales_worker if ever_sample == 1 & young25_08==1 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(es6)
estimates store g6
event_plot es5 es6, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.20(0.05)0.20) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA2c.png", replace  as(png) name("Graph") 
*Figure A2d
* incumbent near and incumbent far together
csdid ln_sales_worker if ever_sample == 1 & young25_08==0 & exchange_distance_ent<=1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(fs5)
estimates store h5
csdid ln_sales_worker if ever_sample == 1 & young25_08==0 & exchange_distance_ent>1, ivar(ruref) time(year) gvar(fibre_enable_year_ent_lag) 		// need to trim time periods etc
estat event, estore(fs6)
estimates store h6
event_plot fs5 fs6, default_look together trimlag(3) trimlead(3) stub_lag(T+#) stub_lead(T-#)  graph_opt(ylabel(-0.10(0.05)0.10) xlabel(-3(1)3) xtitle(time to enablement))  alpha(0.1) 
graph export "FigureA2d.png", replace  as(png) name("Graph") 



